package com.example.demo.controller;


import com.example.demo.common.ResultAjax;
import com.example.demo.common.SessionUtils;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist")
    public ResultAjax myList(HttpServletRequest request) {
        //1，获取用户的session判断用户是否登录(封装到common的包下)
        UserInfo userInfo = SessionUtils.getUser(request);
        if(userInfo == null) {
            return ResultAjax.fail(-1,"请先登录");
        }
        //2，根据用户id从数据库中查询用户文章
        List<ArticleInfo> list = articleService.getArticlesByUid(userInfo);
        //3，返回前端
        return ResultAjax.succ(list);
    }
}
